package hedron.dialog;

import static hedron.i18n.Internationalization._;
import hedron.Application;
import hedron.db.model.Contact;
import hedron.db.model.ContactComment;
import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CommentViewDialog extends AbstractDialog {

	Contact contact;
	Text commentText;

	public CommentViewDialog(Shell parentShell, Contact contact) {
		super(parentShell);
		this.contact = contact;
	}

	@Override
	public void create() {
		super.create();
		setTitle(_("Dialog.CommentView.Title") + " '" + contact.C_NAME + "'");
		setMessage(_("Dialog.CommentView.Message"), 0);
	}

	@Override
	void createContent(Composite parent) {
		Composite grid = new Composite(parent, 0);
		{
			GridLayout layout = new GridLayout();
			layout.numColumns = 1;
			grid.setLayout(layout);
			GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
			gd.widthHint = 400;
			gd.heightHint = 140;
			grid.setLayoutData(gd);
		}

		{
			StyledText styledText = new StyledText(grid, SWT.WRAP | SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL);
			GridData gdata = new GridData(GridData.FILL_BOTH);
			styledText.setLayoutData(gdata);

			ArrayList<ContactComment> comments = Application.CONTACTS.getContactComments(contact);
			StringBuilder commentText = new StringBuilder();

			ArrayList<Integer> startBold = new ArrayList<Integer>();
			ArrayList<Integer> endBold = new ArrayList<Integer>();

			for (ContactComment comment : comments) {
				startBold.add(commentText.length());
				commentText.append(comment.getDateString());
				endBold.add(commentText.length());
				commentText.append(": ");
				commentText.append(comment.U_FULLNAME).append("\n");
				commentText.append(comment.C_COMMENT);
				commentText.append("\n\n");
			}

			styledText.setText(commentText.toString());
			for (int i = 0; i < startBold.size(); i++) {
				int start = startBold.get(i);
				int end = endBold.get(i);
				StyleRange style = new StyleRange();
				style.start = start;
				style.length = end - start;
				style.fontStyle = SWT.BOLD;
				styledText.setStyleRange(style);

			}
		}
	}

	@Override
	void createFooter(Composite parent) {
		((GridLayout) parent.getLayout()).numColumns = 1;
		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.END, true, false));

		{// set grid layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			grid.setLayout(layout);
		}
		{// cell 1 - empty text
			Label label = new Label(grid, 0);
			label.setText("");
			GridData gdata = new GridData(GridData.FILL, GridData.END, true, false);
			gdata.heightHint = 20;
			label.setLayoutData(gdata);
			label.setFocus();
		}
		{// ok
			Button button = new Button(grid, SWT.PUSH);
			button.setText("OK");
			GridData gdata = new GridData(GridData.END, GridData.END, false, false);
			gdata.heightHint = 20;
			gdata.widthHint = 100;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					setReturnCode(OK);
					close();
				}
			});

			// set button as default
			Shell shell = grid.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
	}
}
